#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.61)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AM_INIT_AUTOMAKE(join_ndn, 1.3.10)
AC_CONFIG_SRCDIR([common.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC

# Get fallback domain from users
# AC_ARG_VAR(FALLBACK_DOMAIN, Setting your fallback_domain)
AC_ARG_ENABLE(home_domain,
[  --enable-home-domain=ARG    Enable home domain (ARG=domain_name)],
enable_fallback=$enableval, enable_fallback="no")

# Checks for libraries.

# FIXME: Replace `main' with a function in `-lccn':
AC_CHECK_LIB([ccn], [ccn_charbuf_putf], [], [
              echo "***Error! You MUST install CCNx library***"
              exit -1
              ])
# FIXME: Replace `main' with a function in `-lcares':
AC_CHECK_LIB([cares], [ares_library_init], [], [
              echo "***Error! You MUST install c-ares library***"
              exit -1
              ])
# FIXME: Replace `main' with a function in `-lcrypto':
AC_CHECK_LIB([crypto], [PEM_read_bio_X509], [], [
              echo "***Error! You MUST install openssl library***"
              exit -1
              ])

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([arpa/inet.h netdb.h netinet/in.h stddef.h stdlib.h string.h sys/ioctl.h sys/socket.h sys/time.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_TYPE_UINT16_T
AC_TYPE_UINT32_T
AC_TYPE_UINT8_T

# Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MALLOC
AC_FUNC_SELECT_ARGTYPES
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([gettimeofday memset select socket strerror])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

# check if the keystore exists
CCNXDIR=$HOME/.ccnx
KEYSTORE=$HOME/.ccnx/.ccnx_keystore
if [[ ! -f $KEYSTORE ]]; then
	ccninitkeystore
fi

# Configure environment
# install join_ndn.conf
# HOMECFGDIR=$HOME/.config
# test -d $HOMECFGDIR || mkdir -p $HOMECFGDIR 

echo "# This file sets the home domain for join_ndn." > $CCNXDIR/join_ndn.conf
echo "# If multiple domains provided, client only accepts the first one." >> $CCNXDIR/join_ndn.conf
echo "# **Note that each line only contains one domain.**" >> $CCNXDIR/join_ndn.conf
echo "" >> $CCNXDIR/join_ndn.conf
echo "# netsec.colostate.edu">> $CCNXDIR/join_ndn.conf
if [[ "x$enable_fallback" != "xno" ]]; then
	echo "$enable_fallback" >> $CCNXDIR/join_ndn.conf
fi

echo "========================="
echo "Configure is OK, run make"
echo "========================="
